<script setup lang="ts">

import {ref, onMounted} from 'vue'
import { Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import router from '@/router'
import {jumpToStandardCreate} from '@/utils/jump'
import {apiStandardsList, apiStandardsRetry} from '@/api/api'
import {formatAnalyzeStatus,formatTagAnalyzeStatus} from '@/utils/format'


const searchKey = ref({
  // analyzeStatus : 4,
  // majorName :"",
  // standardName :"",
})
const pageConfig = ref({
  pageNum : 1,
  pageSize :10,
  total:0,

})

const standardList = ref([])
const analyseStatus = [
  {
    value: 3,
    label: '成功',
  },
  {
    value: 2,
    label: '失败',
  },
  {
    value: 1,
    label: '分析中',
  },
  {
    value: 0,
    label: '未开始',
  },
] 




async function handlerStandardList() {
  const req = {
    pageNum:pageConfig.value.pageNum,
    pageSize:pageConfig.value.pageSize,
  }
  if (searchKey.value.majorName){
    req.majorName  = searchKey.value.majorName
  }
if (searchKey.value.analyzeStatus){
    req.analyzeStatus  = searchKey.value.analyzeStatus
  }
  if (searchKey.value.standardName){
    req.standardName  = searchKey.value.standardName
  }


  const data = await apiStandardsList(req)
  if (data.code != 0) {
      console.log(data)
      ElMessage.error("执行失败， 请联系管理员")
      return 
  }

  pageConfig.value.pageNum = data.pageNum
  pageConfig.value.total = data.total
  standardList.value = data.rows
}

const handleDetailClick = (item) => {
  router.push({ path: '/standard/detail', query: { "id": item.id } })
}

async function handleRetryClick(item) {
  const data = await  apiStandardsRetry(item.id)
  if (data.code != 0) {
      console.log(data)
      ElMessage.error("执行失败， 请联系管理员")
      return 
  }else {
      ElMessage.error("重启分析任务成功")
    }

}


onMounted(() => {
  handlerStandardList()
})

</script>

<template>
  
  <el-row :gutter="20" justify="space-between" >
    <el-col :span="4">
      <el-button type="primary" @click="jumpToStandardCreate" >导入</el-button>
    </el-col>
    <el-col :span="16">
      <el-row justify="end" :gutter="10">
        <el-col  :span="4"> 
            <el-input v-model="searchKey.standardName" placeholder="标准名称" clearable />
        </el-col>
        <el-col  :span="6"> 
            <el-input v-model="searchKey.majorName" placeholder="专业" clearable />
        </el-col>
        <el-col  :span="4"> 
            <el-select v-model="searchKey.analyzeStatus"  placeholder="分析状态" clearable >
            <el-option
              v-for="item in analyseStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-col>
        <el-col :span="2"> 
          <el-button   class='pagination' type="primary" :icon="Search" @click="handlerStandardList" >查询</el-button>
        </el-col>
      </el-row>
    </el-col>

  </el-row>

<div class="info_box">

  <el-table :data="standardList" style="width: 100%">
    <el-table-column prop="standardName" label="名称" />
    <el-table-column  label="专业" >
      <template  #default="item">
        {{item.row.mainMajorName}}/{{item.row.subMajorName}}/{{item.row.majorName}}
      </template>
    </el-table-column>
    <el-table-column prop="analyzeStatus" label="分析状态" width="180" >
      <template  #default="item">
          <el-tag  :type="formatTagAnalyzeStatus(item.row.analyzeStatus)">
            {{formatAnalyzeStatus(item.row.analyzeStatus)}}
          </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" width="180" />
    <el-table-column label="操作" width="180" >
        <template  #default="item">
          <el-button link type="primary" v-if="item.row.analyzeStatus == 3"  @click="handleDetailClick(item.row)">详情</el-button>
          <el-button link type="warning" v-if="item.row.analyzeStatus == 2" @click="handleRetryClick(item.row)">重试</el-button>
          <el-button link type="danger" >删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  
  <el-pagination class='pagination' size="large" :page-size="pageConfig.pageSize" layout=" prev, pager, next, total" :total="pageConfig.total"
      v-model:current-page="pageConfig.pageNum" @current-change="handlerStandardList" />

</div>


</template>

<style scoped>

.pagination {
  float: right;
}
</style>